<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>用户管理界面</title>
<!--    <link ref="stylesheet" href="./css/bootstrap.min.css">-->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<!--
导航
-->
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">

            <a class="navbar-brand" href="#">用户管理系统</a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">欢迎：xxx</a></li>
            </ul>
        </div>
    </div>
</nav>

<!--
中心布局
-->
<div id="app">
    <div class="container-fluid">
    <!--    搜索框-->
        <div class="row">
            <div class="col-md-8 col-md-offset-1">
                <form class="form-inline">
                    <div class="form-group">
                        <label for="searchusername"  >姓名</label>
                        <input type="text" class="form-control" v-model="user.name" id="searchusername">
                    </div>
                    <div class="form-group">
                        <label for="searchtel">电话</label>
                        <input type="text" class="form-control"  v-model="user.tel" id="searchtel">
                    </div>
                    <button type="submit" class="btn btn-info"   @click="searchUser">搜索</button>
                </form>
            </div>
        </div>
        <div class="row" style="margin-top: 20px;">
            <div class="col-md-8">
    <!--            用户信息展示-->
                <table class="table table-striped  table-hover">
                    <tr>
                        <th>id</th>
                        <th>姓名</th>
                        <th>年龄</th>
                        <th>班级</th>
                        <th>电话</th>
                        <th>密码</th>
                        <th>操作</th>
                    </tr>
                    <tbody>
                    <tr v-for="(stu,index) in students" :key="stu.id">
                        <td>{{ stu.id }}</td>
                        <td>{{ stu.name }}</td>
                        <td>{{ stu.age }}</td>
                        <td>{{ stu.clazz }}</td>
                        <td>{{ stu.tel }}</td>
                        <td>{{ stu.password }}</td>
                        <td><button class="btn btn-danger" @click="deletebyid(stu.id)">删除</button>&nbsp;&nbsp;<button class="btn btn-primary" @click="editStu(stu)">修改</button>&nbsp;&nbsp;<button class="btn btn-success" @click="showform">添加</button></td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <div class="col-md-4" v-show="formShow">
                <form>
                    <div class="form-group">
                        <label for="username">姓名</label>
                        <input type="text" class="form-control" id="username" v-model="user.name" placeholder="请输入姓名">
                    </div>
                    <div class="form-group">
                        <label for="age">年龄</label>
                        <input type="text" class="form-control" id="age" v-model="user.age" placeholder="年龄">
                    </div>
                    <div class="form-group">
                        <label for="clazz">班级</label>
                        <input type="text" class="form-control" id="clazz" v-model="user.clazz" placeholder="班级">
                    </div>
                    <div class="form-group">
                        <label for="tel">电话</label>
                        <input type="text" class="form-control" id="tel" v-model="user.tel" placeholder="请输入电话">
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" class="form-control" id="password" v-model="user.password" placeholder="请输入密码">
                    </div>
                    <button type="button" class="btn btn-success" v-if="idShow" @click="saveStu">提交</button>
                    <button type="button" class="btn btn-success" v-if="!idShow" @click="editStuBtn">修改</button>
                    <button type="button" class="btn btn-danger" @click.prevent="userDefault">重置</button>
                </form>
            </div>
        </div>
    </div>
</div>
</body>
<script src="./js/vue.js"></script>
<script src="./js/axios.js"></script>
<script>

    const app = new Vue({
        el: "#app",
        data: {
            //来源于后端的接口，在页面加载完成之后就必须完成赋值
            students:[],
            //用于数据双向绑定
            user:{},
            idShow:true,
            formShow:false,
        },
        methods: {
            saveStu() {
                let _this = this;
                console.log(this.user);
                axios.put("http://localhost:8080/student/save",this.user)
                .then(function (response){
                    console.log(response.data);
                    _this.findAll();
                    _this.user = {};
                    _this.formShow = false;
                })
                .catch(function (error){
                    console.log(error);
                })

            },
            deletebyid(id) {
                let _this = this;
                axios.delete("http://localhost:8080/student/delete", {
                    params:{
                        'id':id
                    }
                })
                .then(function (response){
                    console.log(response.data);
                    _this.findAll();
                })
                .catch(function (error){
                    console.log(error);
                })
            },
            //查询所有用户信息的函数
            findAll(){
                let _this = this
                //发送GET方式请求
                axios.get("http://localhost:8080/student/select/all")
                    .then(function (response){
                        console.log(response.data);
                        _this.students = response.data.data;
                    })
                    .catch(function (error){
                        console.log(error);
                    })
            },
            //重置表单数据
            userDefault() {
                this.user={};
            },
            editStu(stu) {
                this.user = stu;
                this.idShow = false;
                this.formShow = !this.formShow;
            },
            editStuBtn() {
                let _this = this;
                axios.patch("http://localhost:8080/student/edit",_this.user)
                    .then(function (response){
                        console.log(response.data);
                        _this.findAll();
                        _this.user = {};
                        _this.idShow = true;
                        _this.formShow = false;
                    })
                    .catch(function (error){
                        console.log(error);
                    })
            },
            showform() {
                this.formShow = !this.formShow;
                this.idShow = true;
            },
            searchUser() {
                let _this = this;
                axios.post("http://localhost:8080/student/select/list",_this.user)
                    .then(function (response){
                        console.log(response.data);
                        _this.students = response.data.data;
                    })
                    .catch(function (error){
                        console.log(error);
                    })
            }
        },
        //生命周期相关函数
        created() {
            this.findAll();
        },
        mounted() {

        }

    });
</script>
</html>
